from pathlib import Path

from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi.security import HTTPBearer
from fastapi.staticfiles import StaticFiles
from starlette.middleware.cors import CORSMiddleware
from tortoise.contrib.fastapi import register_tortoise
from app.settings import TORTOISE_ORM
from app.api.user import user_api
from app.api.cartItem import cartItem_api
from app.api.categories import coffeeCategory_api
from app.api.products import coffeeProduct_api
from app.api.favorite import favorite_api
from app.api.categories import merchandiseCategory_api
from app.api.products import merchandiseProduct_api
from app.api.order import order_api
from app.api.orderItem import orderItem_api
from app.api.productRelation import productRelation_api
from app.api.productReview import productReview_api
from app.api.productTag import productTag_api
from app.api.userAddress import userAddress_api
from app.api.auth import router

load_dotenv()

app = FastAPI(title="中国云咖电商平台后端API服务",version="v1.0.0")

# 允许的源列表
origins = [
"http://localhost",
"http://localhost:8088",
]

# 添加 CORS 中间件
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

app.security = [HTTPBearer()]
app.include_router(router,tags=["中国云咖auth接口"])
app.include_router(user_api,tags=["中国云咖用户接口"])
app.include_router(userAddress_api,tags=["中国云咖用户地址接口"])

app.include_router(coffeeCategory_api,tags=["中国云咖咖啡豆分类接口"])
app.include_router(coffeeProduct_api,tags=["中国云咖咖啡豆产品接口"])

app.include_router(productTag_api,tags=["中国云咖产品标签接口"])
app.include_router(productReview_api,tags=["中国云咖产品评论接口"])
app.include_router(productRelation_api,tags=["中国云咖产品关联接口"])

app.include_router(favorite_api,tags=["中国云咖收藏夹接口"])

app.include_router(cartItem_api,tags=["中国云咖购物车接口"])
app.include_router(orderItem_api,tags=["中国云咖订单项接口"])
app.include_router(order_api,tags=["中国云咖订单接口"])

app.include_router(merchandiseCategory_api,tags=["中国云咖周边商品分类接口"])
app.include_router(merchandiseProduct_api,tags=["中国云咖周边商品产品接口"])

register_tortoise(app=app,config=TORTOISE_ORM)

# 获取项目根目录的绝对路径
BASE_DIR = Path(__file__).resolve().parent.parent

# 静态文件目录路径
STATIC_DIR = BASE_DIR/"static"

# 挂载静态文件目录
app.mount("/static",StaticFiles(directory=STATIC_DIR),name="static")

if __name__ == '__main__':
    import uvicorn


    uvicorn.run("main:app",host="127.0.0.1",port=8000,reload=True)

